package com.bkhech.boot.configure.security.core.handler;

import com.bkhech.boot.configure.common.dto.ResultResponse;
import com.bkhech.boot.configure.common.util.servlet.ServletUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功时返回json数据
 *
 * @author guowm
 * @date 2023/1/10
 */
public class CustomizeAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        System.out.println("==========CustomizeAuthenticationSuccessHandler============");
        System.out.println(authentication);
        //SecurityContextHolder.getContext()
        final Object principal = authentication.getPrincipal();
        ServletUtils.writeJSON(response, ResultResponse.success(principal));
    }
}
